//
//  MDOtherCmdViewController.m
//  MideaWorld_Zizhu
//
//  Created by Minus on 5/28/15.
//  Copyright (c) 2015 Minus. All rights reserved.
//

#import "MDOtherCmdViewController.h"
#import "MDXPGApplianceApi.h"
#import "MDUserDefaults.h"

#import "IDataBodyDevStauts.h"
#import "CurveSleep.h"
#import "Elec.h"
#import "QueryElecCell.h"

#import <java/util/List.h>
#import <java/util/ArrayList.h>

@interface MDOtherCmdViewController ()
{
    MDXPGApplianceApi *_api;
    NSString *_applianceID;
    
    NSArray *_curveArray;   // 睡眠曲线列表
    
    NSArray *_appointArray;     // 预约列表
}

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UILabel *labelResult;

@end

@implementation MDOtherCmdViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _api = [MDXPGApplianceApi getInstance];
    _applianceID = [MDUserDefaults sharedInstance].currentApplianceID;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// 查询睡眠曲线
- (IBAction)actionQueryCosySleep:(id)sender
{
    // 模式
    int coldMode = IDataBodyDevStauts_get_MODE_COLD();  // 制冷模式
    int heatMode = IDataBodyDevStauts_get_MODE_HEAT();  // 制热模式
    
    // 获取睡眠曲线
    [_api getSleepCurveStatus:_applianceID
                         mode:coldMode completion:^(BaseMessage *baseMessage, NSArray *curveSleeps) {
        
        if ([baseMessage getCode] == 0)     // 查询成功
        {
            // 将曲线存起来
            _curveArray = [NSArray arrayWithArray:curveSleeps];
            
            for (CurveSleep *curve in curveSleeps)
            {
                NSLog(@"曲线ID: %d", [curve getCurveId]);
                NSLog(@"曲线模式: %d", [curve getCurveMode]);  // 模式：制冷/制热
                NSLog(@"曲线类型: %d", [curve getCurveType]);
                NSLog(@"曲线状态: %d", [curve getCurveStatus]);     // ON/OFF
                
                // 曲线10个值
                NSLog(@"value0: %d", [curve getValue0]);
                NSLog(@"value1: %d", [curve getValue1]);
                NSLog(@"value2: %d", [curve getValue2]);
                NSLog(@"value3: %d", [curve getValue3]);
                NSLog(@"value4: %d", [curve getValue4]);
                NSLog(@"value5: %d", [curve getValue5]);
                NSLog(@"value6: %d", [curve getValue6]);
                NSLog(@"value7: %d", [curve getValue7]);
                NSLog(@"value8: %d", [curve getValue8]);
                NSLog(@"value9: %d", [curve getValue9]);
                
                /**
                 *  获取曲线10个值的另一种方式
                 */
                IOSFloatArray *values = [curve getValues];
                for (int i = 0; i < [values length]; i++)
                {
                    NSLog(@"value %d: %f", i, [values floatAtIndex:i]);
                }
            }
        }
        else    // 失败
        {
            NSLog(@"查询睡眠曲线失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 更新睡眠曲线
- (IBAction)actionUpdateCosySleep:(id)sender
{
    // 10个温度值（必须是10个值）
    float nums[] = {27.0, 27.0, 27.0, 27.0, 27.0, 27.0, 27.0, 27.0, 27.0, 27.0};
    IOSFloatArray *values = [IOSFloatArray arrayWithLength:10];
    for (int i = 0; i < 10; i++)
    {
        *IOSFloatArray_GetRef(values, i) = nums[i];
    }
    
    // 自己获取到CurveSleep对象
    CurveSleep *curve = nil;
    
    // 更新舒睡曲线的十个值
    [curve setValuesWithFloatArray:values];
    
    [_api updateSleepCurve:_applianceID
                curveSleep:curve completion:^(BaseMessage *baseMessage) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"更新睡眠曲线成功");
        }
        else
        {
            NSLog(@"更新睡眠曲线失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 开启舒睡
- (IBAction)actionStartCosySleep:(id)sender
{
    CurveSleep *curve;
    
    if ([_curveArray count] > 0)
    {
        curve = _curveArray[0];
    }
    else
    {
        float nums[] = {27.0, 27.0, 27.0, 27.0, 27.0, 27.0, 27.0, 27.0, 27.0, 27.0};
        IOSFloatArray *values = [IOSFloatArray arrayWithLength:10];
        for (int i = 0; i < 10; i++)
        {
            *IOSFloatArray_GetRef(values, i) = nums[i];
        }
        
        curve = [[CurveSleep alloc] init];
        [curve init__WithFloatArray:values withInt:IDataBodyDevStauts_get_MODE_COLD()];
    }
    
    // 开始睡眠曲线
    [_api startCurveSleep:_applianceID
               curveSleep:curve completion:^(BaseMessage *baseMessage) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"开始睡眠曲线成功");
        }
        else
        {
            NSLog(@"开始睡眠曲线失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 停止舒睡
- (IBAction)actionStopCosySleep:(id)sender
{
    [_api stopCurveSleep:_applianceID completion:^(BaseMessage *baseMessage) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"停止睡眠曲线成功");
        }
        else
        {
            NSLog(@"停止睡眠曲线失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 查询电量
- (IBAction)actionQueryElec:(id)sender
{
    // 当前只需要月
//    int typeYear = IDataBodyDevStauts_get_POWER_YEAR();     // 查询周期-年
    int typeMonth = IDataBodyDevStauts_get_POWER_MONTH();   // 查询周期-月
//    int typeWeek = IDataBodyDevStauts_get_POWER_WEEK();     // 查询周期-周
    
    int year = 2015;
    int month = 2;
    int day = 1;
    
    [[MDXPGApplianceApi getInstance] queryElec:_applianceID     // 设备ID
                                          type:typeMonth        // 请求类型：年、月、周
                                          year:year             // 年
                                         month:month            // 月
                                           day:day              // 日
                                    completion:^(BaseMessage *baseMessage, Elec *elec) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"用电总时: %lf", [elec getTotalHour]);
            NSLog(@"用电总量: %lf", [elec getTotalValue]);
            
            /**
             *  查询月份的每一天的用电量
             *  cells 的个数是查询的月份的天数，值可能为28、29、30、31
             */
            id<JavaUtilList> cells = [elec getCells];
            
            for (QueryElecCell *cell in cells)
            {
                NSLog(@"天: %d", [cell getDateKey]);
                NSLog(@"电量: %@", [cell getElecValue]);
            }
            
            NSString *date = [NSString stringWithFormat:@"%d-%02d-%02d", year, month, day];
            
            NSLog(@"请求日期: %@", date);
            NSLog(@"月份天数: %d", [[elec getCells] size]);
        }
        else
        {
            NSLog(@"查询电量失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 查询预约
- (IBAction)actionQueryAppoint:(id)sender
{
    [_api getAppointList:_applianceID completion:^(BaseMessage *baseMessage, NSArray *deviceAppoints) {
        
        if ([baseMessage getCode] == 0)
        {
            // 保存预约列表
            _appointArray = [NSArray arrayWithArray:deviceAppoints];
            
            for (DeviceAppoint *appoint in deviceAppoints)
            {
                // 预约开/关
                NSString *switcher = [appoint isSwitcher] ? @"预约开" : @"预约关";
                // 预约设备的启动状态：有效/无效
                NSString *status = [appoint isStatus] ? @"有效" : @"无效";
                // 重复
                NSString *repeat = [appoint isRepeat] ? @"重复" : @"不重复";
                /**
                 *  预约星期
                 *  返回值举例:   1100101 -> 代表周一、二、五、日开启预约
                 *              1110000 -> 代表周一、二、三开启预约
                 */
                NSString *week = [appoint getWeek];
                
                NSLog(@"预约ID: %@", [appoint getApplianceAppointId]);
                NSLog(@"预约开/关: %@", switcher);
                NSLog(@"预约时间: %@", [appoint getTime]);
                NSLog(@"预约星期: %@", week);
                NSLog(@"预约设备的状态: %@", status);
                NSLog(@"重复: %@", repeat);
            }
        }
        else
        {
            NSLog(@"查询预约失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 新增预约
- (IBAction)actionAddAppoint:(id)sender
{
    // 预约开＝YES，预约关＝NO
    BOOL switcher = YES;
    
    // 预约时间，格式：“09:15”，只需要传入手机的本地时间则可，SDK会将时间转换为UTC时间
    NSString *time = @"08:05";
    
    /**
     *  预约星期
     *  值举例:      1357 -> 代表周一、三、五、日开启预约
     *              12345 -> 代表周一、二、三、四、五开启预约
     */
    NSString *week = @"1234567";
    
    // 设备启动状态：有效＝YES，无效＝NO
    BOOL status = YES;
    
    // 重复：重复=YES，不重复＝NO
    BOOL repeat = YES;
    
    // 创建 DeviceAppoint 对象
    DeviceAppoint *appoint = [[DeviceAppoint alloc] init];
    // 初始化 DeviceAppoint 对象
    [appoint init__WithNSString:_applianceID    // 设备ID
                    withBoolean:switcher        // 预约开/关
                   withNSString:time            // 预约时间
                   withNSString:week            // 预约星期
                    withBoolean:status          // 设备启动状态
                    withBoolean:repeat];        // 重复
    
    // 或者通过以下方法设置值
    /*
    [appoint setApplianceIdWithNSString:_applianceID];  // 设备ID
    [appoint setSwitcherWithBoolean:switcher];          // 预约开/关
    [appoint setTimeWithNSString:time];                 // 预约时间
    [appoint setWeekWithNSString:week];                 // 预约星期
    [appoint setStatusWithBoolean:status];              // 设备启动状态
    [appoint setRepeatWithBoolean:repeat];              // 重复
     */
    
    NSLog(@"---->> %@", appoint);
    
    [_api addDeviceAppoint:appoint completion:^(BaseMessage *baseMessage, NSString *appointID) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"新增预约成功");
            NSLog(@"预约ID: %@", appointID);
        }
        else
        {
            NSLog(@"新增预约失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 更新预约
- (IBAction)actionUpdateAppoint:(id)sender
{
    DeviceAppoint *appoint = nil;
    
    if ([_appointArray count] > 0)
    {
        appoint = _appointArray[0];
        
        [appoint setTimeWithNSString:@"12:55"];
        [appoint setWeekWithNSString:@"12345"];
        [appoint setRepeatWithBoolean:YES];
        
        [_api updateDeviceAppoint:appoint completion:^(BaseMessage *baseMessage) {
            
            if ([baseMessage getCode] == 0)
            {
                NSLog(@"更新预约成功");
            }
            else
            {
                NSLog(@"更新预约失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
            }
        }];
    }
}

// 开启预约
- (IBAction)actionStartAppoint:(id)sender
{
    if ([_appointArray count] > 0)
    {
        DeviceAppoint *appoint = _appointArray[0];
        
        [_api startAppoint:[appoint getApplianceAppointId] completion:^(BaseMessage *baseMessage) {
            
            if ([baseMessage getCode] == 0)
            {
                NSLog(@"开启预约成功");
            }
            else
            {
                NSLog(@"开启预约失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
            }
        }];
    }
}

// 停止预约
- (IBAction)actionStopAppoint:(id)sender
{
    if ([_appointArray count] > 0)
    {
        DeviceAppoint *appoint = _appointArray[0];
        
        [_api stopAppoint:[appoint getApplianceAppointId] completion:^(BaseMessage *baseMessage) {
            
            if ([baseMessage getCode] == 0)
            {
                NSLog(@"停止预约成功");
            }
            else
            {
                NSLog(@"停止预约失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
            }
        }];
    }
}

// 开始体检
- (IBAction)actionStartCheck:(id)sender
{
    [_api startACCheck:_applianceID completion:^(BaseMessage *baseMessage, CheckDetails *checkDetails) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"体检成功");
            
            NSLog(@"最后一次体检时间: %@", [checkDetails getLastCheckTime]);
            NSLog(@"体检项（综述）: %d", [checkDetails getCheckNum]);
            NSLog(@"正常项: %d", [checkDetails getNormNum]);
            NSLog(@"严重错误项: %d", [checkDetails getSeriousFault]);
            NSLog(@"一般错误项: %d", [checkDetails getGeneralFailure]);
            NSLog(@"轻微错误项: %d", [checkDetails getMinorFault]);
            NSLog(@"体检建议描述: %@", [checkDetails getProposeDes]);
            NSLog(@"建议操作: %d", [checkDetails getProposeAction]);
        }
        else
        {
            NSLog(@"体检失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 获取体检详情
- (IBAction)actionGetCheckInfo:(id)sender
{
    [_api getACCheckDetails:_applianceID completion:^(BaseMessage *baseMessage, NSArray *checkItemDetails) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"获取体检详情成功");
            
            for (AcCheckItemDetails *checkItem in checkItemDetails)
            {
                // 体检项目
                NSString *name = [checkItem getParameterName];
                // 当前值（在最小值和最大值之间是正常）
                int realValue = [checkItem getRealValue];
                // 最小值
                int minValue = [checkItem getMinValue];
                // 最大值
                int maxValue = [checkItem getMaxValue];
                // 状态 1-正常，2-严重故障，3-一般故障，4-轻微故障
                int status = [checkItem getStatus];
                
                NSLog(@"体检项目：%@", name);
                NSLog(@"当前值：%d", realValue);
                NSLog(@"最小值：%d", minValue);
                NSLog(@"最大值：%d", maxValue);
                NSLog(@"状态：%d", status);
            }
        }
        else
        {
            NSLog(@"获取体检详情失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 查询T1温度推送设置
- (IBAction)actionQueryT1Temperature:(id)sender
{
    [_api queryT1Temperature:_applianceID completion:^(BaseMessage *baseMessage, BOOL onoff) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"查询T1温度推送成功");
            
            if (onoff)
            {
                NSLog(@"推送开启");
            }
            else
            {
                NSLog(@"推送关闭");
            }
        }
        else
        {
            NSLog(@"查询T1温度推送失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 增加T1温度推送设置
- (IBAction)actionAddT1Temperature:(id)sender
{
    NSString *time = @"12:05";      // 推送时间，格式："HH:mm"，UTC时间
    NSString *week = @"123";    // 推送周，123 代表周一二三推送，四五六日不推送
    int repeat = 1;                 // 重复，1-重复，0-不重复
    
    [_api addT1Temperature:_applianceID
                      time:time
                      week:week
                    repeat:repeat completion:^(BaseMessage *baseMessage) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"增加T1温度推送成功");
        }
        else
        {
            NSLog(@"增加T1温度推送失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 更新T1温度推送设置
- (IBAction)actionUpdateT1:(id)sender
{
    NSString *time = @"12:05";      // 推送时间，格式："HH:mm"，UTC时间
    NSString *week = @"12345";    // 推送周，12345 代表周一二三四五推送，六日不推送
    int repeat = 1;                 // 重复，1-重复，0-不重复
    
    [_api updateT1Temperature:_applianceID
                         time:time
                         week:week
                       repeat:repeat completion:^(BaseMessage *baseMessage) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"更新T1温度推送设置成功");
        }
        else
        {
            NSLog(@"更新T1温度推送设置失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 修改用户信息
- (IBAction)actionModifyUserInfo:(id)sender
{
    // 用户昵称
    NSString *nickName = @"Minus";
    
    [_api userModifyInfo:nickName completion:^(BaseMessage *baseMessage) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"修改用户信息成功");
        }
        else
        {
            NSLog(@"修改用户信息失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 修改设备信息
- (IBAction)actionModifyDeviceInfo:(id)sender
{
    // 设备ID
    NSString *applianceID = [MDUserDefaults sharedInstance].currentApplianceID;
    // 自定义任意设备名
    NSString *applianceName = @"ACW007";
    
    [_api deviceModifyInfo:applianceID applianceName:applianceName completion:^(BaseMessage *baseMessage) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"修改设备信息成功");
        }
        else
        {
            NSLog(@"修改设备信息失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}


// 检测更新
- (IBAction)actionCheckUpdate:(id)sender
{
    [_api checkForUpdate:^(BaseMessage *baseMessage, NSArray *versionInfos) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"检测更新成功");
            
            for (VersionInfo *info in versionInfos)
            {
                NSLog(@"版本号: %@", [info getVersionCode]);
                NSLog(@"app类型: %@", [info getAppType]);
            }
        }
        else
        {
            NSLog(@"检测更新失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

// 获取更新
- (IBAction)actionGetUpdate:(id)sender
{
    NSString *versionCode = @"10.0";
    
    [_api getLastestAppInfo:versionCode completion:^(BaseMessage *baseMessage, AppInfo *appInfo) {
        
        if ([baseMessage getCode] == 0)
        {
            NSLog(@"获取更新成功");
            
            NSLog(@"版本号: %@", [appInfo getVersionCode]);
            NSLog(@"版本名: %@", [appInfo getVersionName]);
            NSLog(@"描述: %@", [appInfo getDes]);
            NSLog(@"包大小: %@", [appInfo getSize]);
            NSLog(@"更新时间: %@", [appInfo getUpdateTime]);
            NSLog(@"下载地址: %@", [appInfo getDownloadURL]);
        }
        else
        {
            NSLog(@"获取更新失败. [%d]:%@", [baseMessage getCode], [baseMessage getMessage]);
        }
    }];
}

@end
